package thu.sorit.resolver;

import java.util.HashMap;
import java.util.List;

import thu.sorit.R;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoAdaptor extends BaseAdapter {

	private List<Video> list;
	
	private LayoutInflater inflater;
	
	public VideoAdaptor(LayoutInflater inflater, List<Video> list) {
		this.list = list;
		this.inflater = inflater;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
        if(v == null)
            v = inflater.inflate(R.layout.list_item, null);
        ImageView preview = (ImageView) v.findViewById(R.id.list_image);
        TextView title = (TextView) v.findViewById(R.id.list_title);
        TextView url = (TextView) v.findViewById(R.id.list_url);
        TextView duration = (TextView) v.findViewById(R.id.list_duration);
        preview.setImageBitmap(list.get(position).preview);
        title.setText(list.get(position).title);
        url.setText(list.get(position).url);
        duration.setText(getTimeDesc(list.get(position).duration));
        return v;
	}

	private String getTimeDesc(int duration) {
		StringBuilder sb = new StringBuilder("时长：");
		int hour = duration / 3600;
		int minute = (duration % 3600) / 60;
		int second = duration % 60;
		if (hour > 0) {
			sb.append(hour).append("小时");
		}
		if (hour != 0 || minute != 0) {
			sb.append(minute).append("分");
		}
		sb.append(second).append("秒");
		return sb.toString();
	}

}
